package com.matt.repository.impl;

import com.matt.foundation.DO.MeetingDO;
import com.matt.foundation.query.MeetingQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.MeetingLinkRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/6/29
 * *******************************
 */
@Component
public class MeetingLinkRepositoryImpl extends BaseNativeSqlRepository implements MeetingLinkRepository{

    @Override
    public List<MeetingDO> findByCondition(MeetingQuery query) {
        String querySql = String.format("select m.id as id, m.startDate as startDate, m.title as title, t.name as teachingAreaName, mt.name as mType "
                + " from meeting m LEFT join teaching_area t on m.taid = t.id left join meeting_type mt on m.mType = mt.id " +
                " WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and m.aid='%s' ", query.getAreaId());
        }

        querySql += " order by m.createTime desc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());
        List<MeetingDO> objectArrayList = sqlObjectList(querySql, MeetingDO.class);
        return objectArrayList;
    }

    @Override
    public Long countByCondition(MeetingQuery query) {
        String querySql = String.format("select count(m.id) "
                + " from meeting m  " +
                " WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and m.aid='%s' ", query.getAreaId());
        }

        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
